/**
 * Copyright (c) 2015-2017, Henry Yang 杨勇 (gismail@foxmail.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mapfinal.server.auth;

import com.jfinal.config.Interceptors;
import com.jfinal.config.Routes;
import com.jfinal.kit.StrKit;
import com.jfinal.template.Engine;
import com.lambkit.Lambkit;
import com.lambkit.common.service.ServiceManager;
import com.lambkit.core.rpc.RpcConfig;
import com.lambkit.db.datasource.ActiveRecordPluginWrapper;
import com.lambkit.module.LambkitModule;
import com.mapfinal.server.MapfinalServerConfig;
import com.mapfinal.server.auth.model.AuthAccess;
import com.mapfinal.server.auth.model.AuthJob;
import com.mapfinal.server.auth.model.AuthOrganization;
import com.mapfinal.server.auth.model.AuthRule;
import com.mapfinal.server.auth.model.RoleUser;
import com.mapfinal.server.auth.model.Role;
import com.mapfinal.server.auth.model.UserOrganization;
import com.mapfinal.server.auth.model.User;
import com.mapfinal.server.auth.service.impl.AuthAccessServiceImpl;
import com.mapfinal.server.auth.service.impl.AuthAccessServiceMock;
import com.mapfinal.server.auth.service.impl.AuthJobServiceImpl;
import com.mapfinal.server.auth.service.impl.AuthJobServiceMock;
import com.mapfinal.server.auth.service.impl.AuthOrganizationServiceImpl;
import com.mapfinal.server.auth.service.impl.AuthOrganizationServiceMock;
import com.mapfinal.server.auth.service.impl.AuthRuleServiceImpl;
import com.mapfinal.server.auth.service.impl.AuthRuleServiceMock;
import com.mapfinal.server.auth.service.impl.RoleUserServiceImpl;
import com.mapfinal.server.auth.service.impl.RoleUserServiceMock;
import com.mapfinal.server.auth.service.impl.RoleServiceImpl;
import com.mapfinal.server.auth.service.impl.RoleServiceMock;
import com.mapfinal.server.auth.service.impl.UserOrganizationServiceImpl;
import com.mapfinal.server.auth.service.impl.UserOrganizationServiceMock;
import com.mapfinal.server.auth.service.impl.UserServiceImpl;
import com.mapfinal.server.auth.service.impl.UserServiceMock;
import com.mapfinal.server.auth.web.AuthAccessDirective;
import com.mapfinal.server.auth.web.AuthAccessMarker;
import com.mapfinal.server.auth.web.AuthDirective;
import com.mapfinal.server.auth.web.AuthJobDirective;
import com.mapfinal.server.auth.web.AuthJobMarker;
import com.mapfinal.server.auth.web.AuthMarker;
import com.mapfinal.server.auth.web.AuthOrganizationDirective;
import com.mapfinal.server.auth.web.AuthOrganizationMarker;
import com.mapfinal.server.auth.web.AuthRuleDirective;
import com.mapfinal.server.auth.web.AuthRuleMarker;
import com.mapfinal.server.auth.web.RoleUserDirective;
import com.mapfinal.server.auth.web.RoleUserMarker;
import com.mapfinal.server.auth.web.RolesDirective;
import com.mapfinal.server.auth.web.RolesMarker;
import com.mapfinal.server.auth.web.UserDirective;
import com.mapfinal.server.auth.web.UserMarker;
import com.mapfinal.server.auth.web.UserOrganizationDirective;
import com.mapfinal.server.auth.web.UserOrganizationMarker;
import com.mapfinal.server.auth.web.UsersDirective;
import com.mapfinal.server.auth.web.UsersMarker;

/**
 * @author yangyong 
 * @website: www.lambkit.com
 * @email: gismail@foxmail.com
 * @date 2020-09-23
 * @version 1.0
 * @since 1.0
 */
public class AuthModule extends LambkitModule  {

	@Override
	public void configMapping(ActiveRecordPluginWrapper arp) {
		if(StrKit.isBlank(getConfig().getDbConfig())) {
			mapping(arp);
		}
	}
	
	@Override
	public void configMapping(String name, ActiveRecordPluginWrapper arp) {
		super.configMapping(name, arp);
		if(StrKit.notBlank(name) && name.equals(getConfig().getDbConfig())) {
			mapping(arp);
		}
	}
	
	@Override
	public void configEngine(Engine me) {
		addDirective(me, "authTag", AuthDirective.class);
		addDirective(me, "userTag", UserDirective.class);
		
		addDirective(me, "authAccess", AuthAccessDirective.class);
		addDirective(me, "authRule", AuthRuleDirective.class);
		addDirective(me, "roleUser", RoleUserDirective.class);
		addDirective(me, "role", RolesDirective.class);
		addDirective(me, "user", UsersDirective.class);
		addDirective(me, "authJob", AuthJobDirective.class);
		addDirective(me, "authOrganization", AuthOrganizationDirective.class);
		addDirective(me, "userOrganization", UserOrganizationDirective.class);
	}
	
	@Override
	public void configRoute(Routes me) {
		me.add("/auth", AuthController.class, "/mapfinal/html/auth");
		me.add("/user", UserController.class, "/mapfinal/html/user");
		me.add("/role", RoleController.class, "/mapfinal/html/user/role");
		me.add("/rule", RuleController.class, "/mapfinal/html/user/rule");
		me.add("/organization", OrganizationController.class, "/mapfinal/html/user/organization");
	}
	
	@Override
	public void configInterceptor(Interceptors me) {
		// TODO Auto-generated method stub
		super.configInterceptor(me);
		//用户权限拦截器
		me.addGlobalActionInterceptor(new AuthInterceptor());
	}

	@Override
	public void onStart() {
		// TODO Auto-generated method stub
		addTag(this);
		if("server".equals(getConfig().getRpcType())) {
			registerLocalService();
		} else if("client".equals(getConfig().getRpcType())) {
			registerRemoteService();
		} 
	}
	
	public void mapping(ActiveRecordPluginWrapper arp) {
		//岗位
		arp.addMapping("mf_auth_job", "id", AuthJob.class);
		//部门
		arp.addMapping("mf_auth_organization", "id", AuthOrganization.class);
		//权限
		arp.addMapping("mf_auth_rule", "id", AuthRule.class);
		//角色
		arp.addMapping("mf_role", "id", Role.class);
		arp.addMapping("mf_role_user", "role_id,user_id", RoleUser.class);
		arp.addMapping("mf_auth_access", "role_id,rule_id", AuthAccess.class);
		//用户
		arp.addMapping("mf_user", "id", User.class);
		arp.addMapping("mf_user_organization", "user_id,organization_id", UserOrganization.class);
	}
	
	public void addTag(LambkitModule lk) {
		lk.addTag("authTag", new AuthMarker());
		lk.addTag("userTag", new UserMarker());
		
		lk.addTag("authAccess", new AuthAccessMarker());
		lk.addTag("authRule", new AuthRuleMarker());
		lk.addTag("roleUser", new RoleUserMarker());
		lk.addTag("role", new RolesMarker());
		lk.addTag("user", new UsersMarker());
		lk.addTag("authJob", new AuthJobMarker());
		lk.addTag("authOrganization", new AuthOrganizationMarker());
		lk.addTag("userOrganization", new UserOrganizationMarker());
	}
			
	public void registerLocalService() {
		registerLocalService(getRpcGroup(), getRpcVersion(), getRpcPort());
	}
	
	public void registerLocalService(String group, String version, int port) {
		ServiceManager.me().mapping(AuthAccessService.class, AuthAccessServiceImpl.class, AuthAccessServiceMock.class, group, version, port);
		ServiceManager.me().mapping(AuthRuleService.class, AuthRuleServiceImpl.class, AuthRuleServiceMock.class, group, version, port);
		ServiceManager.me().mapping(RoleUserService.class, RoleUserServiceImpl.class, RoleUserServiceMock.class, group, version, port);
		ServiceManager.me().mapping(RoleService.class, RoleServiceImpl.class, RoleServiceMock.class, group, version, port);
		ServiceManager.me().mapping(UserService.class, UserServiceImpl.class, UserServiceMock.class, group, version, port);
		ServiceManager.me().mapping(AuthJobService.class, AuthJobServiceImpl.class, AuthJobServiceMock.class, group, version, port);
		ServiceManager.me().mapping(AuthOrganizationService.class, AuthOrganizationServiceImpl.class, AuthOrganizationServiceMock.class, group, version, port);
		ServiceManager.me().mapping(UserOrganizationService.class, UserOrganizationServiceImpl.class, UserOrganizationServiceMock.class, group, version, port);
	}
	
	public void registerRemoteService() {
		registerRemoteService(getRpcGroup(), getRpcVersion(), getRpcPort());
	}
	
	public void registerRemoteService(String group, String version, int port) {
		ServiceManager.me().remote(AuthAccessService.class, AuthAccessServiceMock.class, group, version, port);
		ServiceManager.me().remote(AuthRuleService.class, AuthRuleServiceMock.class, group, version, port);
		ServiceManager.me().remote(RoleUserService.class, RoleUserServiceMock.class, group, version, port);
		ServiceManager.me().remote(RoleService.class, RoleServiceMock.class, group, version, port);
		ServiceManager.me().remote(UserService.class, UserServiceMock.class, group, version, port);
		ServiceManager.me().remote(AuthJobService.class, AuthJobServiceMock.class, group, version, port);
		ServiceManager.me().remote(AuthOrganizationService.class, AuthOrganizationServiceMock.class, group, version, port);
		ServiceManager.me().remote(UserOrganizationService.class, UserOrganizationServiceMock.class, group, version, port);
	}
	
	public int getRpcPort() {
		return Lambkit.config(RpcConfig.class).getDefaultPort();
	}
	public String getRpcGroup() {
		return Lambkit.config(RpcConfig.class).getDefaultGroup();
	}
	public String getRpcVersion() {
		return getConfig().getRpcVersion();
	}
	public MapfinalServerConfig getConfig() {
		return Lambkit.config(MapfinalServerConfig.class);
	}
}

